package myservice.android.utilities;

import myservice.android.R;
import myservice.android.utilities.ObservableScrollView.OnScrollStoppedListener;
import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class NumberPicker extends RelativeLayout {
	
	public interface OnNumberPickerClosedListener {
		public void onEvent();
	}	
	
	public int selected = 0;
	public int maxValue = 99;

	private ObservableScrollView scroll;
	private LinearLayout list;
	private EditText editText;
	private View clickArea;
	private int elementHeight = 0;
	
    private OnNumberPickerClosedListener listener = null;
	
	public NumberPicker(Context context, AttributeSet attrs) {	
		
		super(context, attrs);		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.number_picker, this, true);
		
		scroll = (ObservableScrollView) findViewById(R.id.scroll);
		list = (LinearLayout) findViewById(R.id.list);
		clickArea = (View) findViewById(R.id.clickArea);
		editText = (EditText) findViewById(R.id.editText);
		
	}
	
	public void setOnNumberPickerClosedListener(OnNumberPickerClosedListener listener) {
		this.listener = listener;
	}
	
	private void addElement(Context context, String v) {		
		NumberPickerRow element = new NumberPickerRow(context);
		element.value.setText(v);
		element.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (editText.getVisibility()==VISIBLE) {
					editText.setVisibility(INVISIBLE);	
					scroll.scrollTo(0, getSelected()*getElementHeight());
				}
			}
			
		});
		list.addView(element);		
	}		
	
	public void init(Context c, int maxValue, int selected, boolean twoDigits) {		
		
		final Context context = c;
		
		this.maxValue = maxValue;
		this.selected = selected;		

		addElement(context,"");
		for (int i=0; i<=maxValue; i++) {
			String s = "";
			if (i<10 && twoDigits==true) {
				s = "0";
			}
			addElement(context,s+i);
		}
		addElement(context,"");
		addElement(context,"");		
		
		// Catch the moment where we can calculate the height of a list row 
		scroll.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {								
				elementHeight = list.getChildAt(0).getHeight();	
				scroll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				scroll.scrollTo(0, getSelected()*getElementHeight());
			}
			
		});			
		
		clickArea.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (editText.getVisibility() == INVISIBLE) {
					if (listener!=null)
						listener.onEvent();
				} else {				
					editText.setVisibility(INVISIBLE);
					scroll.scrollTo(0, getSelected()*getElementHeight());
				}
			}		
		});			
		
		scroll.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
	                scroll.startScrollerTask();
	            }

	            return false;
			}
			
		});
		
		scroll.setOnScrollStoppedListener(new OnScrollStoppedListener() {

			@Override
			public void onScrollStopped() {
				snapList();			
			}
			
		});					
		
		clickArea.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				
				if (editText.getVisibility() != VISIBLE) {
				
					editText.setVisibility(VISIBLE);
					editText.setText(""+getSelected());
					InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);						
					editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
					editText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
				}
				return true;
			}
			
		});				
		
		editText.addTextChangedListener(new NumberPickerMonitor(this));			
		
	}	
	
	private int getSelected() {		
		return selected;		
	}
	
	private int getElementHeight() {
		return elementHeight;
	}
	
	private void snapList() {		
		
		int y = scroll.getScrollY();

		int firstElement = y / getElementHeight();
		if (y % elementHeight > getElementHeight()/2)
			firstElement++;				
		
		scroll.scrollTo(0, firstElement*getElementHeight());
		
		selected = firstElement;		
		
	}		

}
